// @charset "utf-8";
/**
 * 新股申购view
 */
define(function (require, exports, module) {

	var xgsgView = {
		delay : 1000,
		count : 0,
		autoIpo : null,
		isSuccess : false,
		hasIpo : false,
		request : function (swtichInit) {
			var self = this;
			var url = '/zx/freeclient/homepage/xgsg.json';

			var bool = commonMethod.thsQuote.requestZxData({
				url: url,
				onready: function(res) {
					self.isSuccess = true;
					if (res == '[]') {
						self.render('NUL');
					} else {
						if (swtichInit) {
							self.render(res, swtichInit);
						} else {
							self.render(res);
						}
					}
				}
			});
			if (!bool || !self.isSuccess) {
				if (self.count > 5) {
					self.delay = 60000;
					self.render('NUL');
				}
				self.autoIpo = setTimeout(function () {
					self.request();
				}, self.delay);
			} else {
				clearTimeout(self.autoIpo);
			}
		},
		render : function (result, swtichInit) {
			var self = this;
			if (result != 'NUL') {
				var resultData = JSON.parse(result);
				var ipoData = resultData.data;
				if (ipoData.length) {
					ipoData.reverse();
					self.hasIpo = true;
					var ipoHtml = [],
						html = '';
					var curtime = commonMethod.serverTime();
					$.each(ipoData, function (i, val) {
						var date;
						if (val.date == curtime) {
							date = '<a href="###" class="apply-btn view_a_apply"></a>';
						} else {
							date = val.date.substr(4, 2) + '-' + val.date.substr(6, 2);
						}
						html = '<tr><td class="text-center">' + val.name +
								'</td><td class="text-center">' + date +
								'</td><td class="text-center">' + val.stockcode +
								'</td><td class="text-right">' + val.price +
								'</td><td class="text-right">' + val.number +
								'</td><td class="text-right pdr10">' + val.limit +
								'</td></tr>';
						ipoHtml.push(html);
					});
					$('#view_tbody_xgsgdata').html(ipoHtml.join(''));
					$('.view_span_xgsgnodate').hide();
					$('#view_table_xgsgdata').show();
				} else {
					self.hasIpo = false;
					$('.view_span_xgsgnodate').show();
					$('#view_table_xgsgdata').hide();
				}
			} else {
				self.hasIpo = false;
				$('.view_span_xgsgnodate').show();
				$('#view_table_xgsgdata').hide();
			}
			swtichInit();
		}
	};

	module.exports = xgsgView;
});